package com.iOS.service.impl;

import com.iOS.dao.FileInfoMapper;
import com.iOS.dao.LoginRecordMapper;
import com.iOS.dao.UserInfoMapper;
import com.iOS.dto.FileInfoExecution;
import com.iOS.entity.FileInfo;
import com.iOS.entity.LoginRecord;
import com.iOS.entity.UserInfo;
import com.iOS.enums.FileInfoEnum;
import com.iOS.service.IFileInfoService;
import com.iOS.util.FileUtil;
import com.iOS.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class FileInfoServiceImpl implements IFileInfoService {
    @Autowired
    private FileInfoMapper fileInfoMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private LoginRecordMapper loginRecordMapper;

    @Override
    public FileInfoExecution add(String token, String fileTitle, InputStream fileInputStream, String filename) {
        try {
            if (loginRecordMapper.checkToken(token) == 0) {
                return new FileInfoExecution(FileInfoEnum.NO_LOGIM);
            } else {
                LoginRecord loginRecord = loginRecordMapper.selectByToken(token);
                int userid = loginRecord.getUserId();
                FileInfo fileInfo = new FileInfo();
                fileInfo.setFileTitle(fileTitle);
                fileInfo.setCreateTime(new Date());
                fileInfo.setUserId(userid);
                fileInfo.setFileUrl(saveFile(fileInputStream, filename));
                int isSuccess = fileInfoMapper.insert(fileInfo);
                if (isSuccess == 1) {
                    return new FileInfoExecution(FileInfoEnum.SUCCESS, fileInfo);
                } else {
                    return new FileInfoExecution(FileInfoEnum.FAIL);
                }
            }
        } catch (Exception e) {
            return new FileInfoExecution(FileInfoEnum.INNER_ERROR);
        }
    }

    @Override
    public FileInfoExecution delete(String token, int id) {
        try {
            if (loginRecordMapper.checkToken(token) == 0) {
                return new FileInfoExecution(FileInfoEnum.NO_LOGIM);
            } else {
                //token拿userid
                LoginRecord loginRecord = loginRecordMapper.selectByToken(token);
                int userid = loginRecord.getUserId();
                //id拿文件记录
                FileInfo fileInfo = fileInfoMapper.queryById(id);
                int userid_1 = fileInfo.getUserId();
                //如果是同一个用户则可以删除
                if (userid == userid_1) {
                    //删除文件
                    String delpath = fileInfo.getFileUrl();
                    java.io.File delFile = new java.io.File(delpath);
                    delFile.delete();
                    //删除记录
                    fileInfoMapper.delete(id);
                    return new FileInfoExecution(FileInfoEnum.SUCCESS);
                } else {
                    return new FileInfoExecution(FileInfoEnum.NO_POWER);
                }
            }
        } catch (Exception e) {
            return new FileInfoExecution(FileInfoEnum.INNER_ERROR);
        }
    }

    @Override
    public FileInfoExecution selectbyId(int id) {
        try {
            if (fileInfoMapper.checkFile(id) == 0) {
                return new FileInfoExecution(FileInfoEnum.NULL);
            } else {
                FileInfo fileInfo = fileInfoMapper.queryById(id);
                if (fileInfo != null) {
                    return new FileInfoExecution(FileInfoEnum.SUCCESS, fileInfo);
                } else {
                    return new FileInfoExecution(FileInfoEnum.FAIL);
                }
            }
        } catch (Exception e) {
            return new FileInfoExecution(FileInfoEnum.INNER_ERROR);
        }
    }

    @Override
    public FileInfoExecution selectByUser(int userId) {
        try {
            if (userInfoMapper.checkId(userId) == 0) {
                return new FileInfoExecution(FileInfoEnum.NULL_USER);
            } else {
                List<FileInfo> fileInfoList = fileInfoMapper.queryByUser(userId);
                if (fileInfoList != null) {
                    return new FileInfoExecution(FileInfoEnum.SUCCESS, fileInfoList);
                } else {
                    return new FileInfoExecution(FileInfoEnum.FAIL);
                }
            }
        } catch (Exception e) {
            return new FileInfoExecution(FileInfoEnum.INNER_ERROR);
        }
    }

    @Override
    public FileInfoExecution selectAll() {
        try {
            List<FileInfo> fileInfoList = fileInfoMapper.queryAll();
            if (fileInfoList != null) {
                return new FileInfoExecution(FileInfoEnum.SUCCESS, fileInfoList);
            } else {
                return new FileInfoExecution(FileInfoEnum.FAIL);
            }
        } catch (Exception e) {
            return new FileInfoExecution(FileInfoEnum.INNER_ERROR);
        }
    }

    /*存储文件并返回路径*/
    private String saveFile(InputStream fileInputStream, String filename) {
        /*获取文件存储目录*/
        String dir = PathUtil.getFilePath();
        String savePath = FileUtil.ManageFile(fileInputStream, filename, dir);
        return savePath;
    }
}
